package com.example.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

/**
 * redis多数据源配置，支持不同的redis服务，不同的database库
 */
@Configuration
public class RedisMultSourceConfig {

    @Value("${spring.redis.database2}")
    private int database2;

    @Value("${spring.redis.host2}")
    private String host2;

    @Value("${spring.redis.port2}")
    private int port2;

    @Value("${spring.redis.password2}")
    private String password2;


    /**
     * 自动装配的redis(第一个redis)
     * @param factory
     * @return
     */
    @Bean(name = "redisTemplate")
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        // 创建RedisTemplate<String,Object>对象
        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        // 配置连接工厂
        template.setConnectionFactory(factory);
        // 设置序列化方式
        template = buildRedisTemplate(template);
        return template;
    }


    /**
     * 第二个redis
     * @return
     */
    @Bean(name = "redisTemplateTwo")
    public RedisTemplate<String, Object> redisTemplateTwo() {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        // 配置连接工厂
        template.setConnectionFactory(redisConnectionFactoryTwo());
        template = buildRedisTemplate(template);
        return template;
    }

    /**
     * 设置database库
     * @return
     */
    public RedisConnectionFactory redisConnectionFactoryTwo(){
        JedisConnectionFactory jedisConnectionFactory = buildJedisConnectionFactory();
        jedisConnectionFactory.setDatabase(database2);
        return jedisConnectionFactory;
    }

    /**
     * 设置第二个redis库连接参数
     * @return
     */
    public JedisConnectionFactory buildJedisConnectionFactory() {
        JedisPoolConfig jedisPoolConfig=new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(8);
        jedisPoolConfig.setMinIdle(0);
        jedisPoolConfig.setTestOnBorrow(true);
        jedisPoolConfig.setTestOnReturn(true);
        jedisPoolConfig.setTestWhileIdle(true);
        jedisPoolConfig.setNumTestsPerEvictionRun(10);
        jedisPoolConfig.setTimeBetweenEvictionRunsMillis(60000);
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(jedisPoolConfig);
        jedisConnectionFactory.setHostName(host2);
        if(!password2.isEmpty()){
            jedisConnectionFactory.setPassword(password2);
        }
        jedisConnectionFactory.setPort(port2);
        return jedisConnectionFactory;
    }

    /**
     * 设置序列化方式
     * @param template
     * @return
     */
    public RedisTemplate<String, Object> buildRedisTemplate(RedisTemplate<String, Object> template) {
        // 定义jackson2JsonRedisSerializer序列化对象
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        // 指定序列化的域，field，get和set，以及修饰符范围，ANY是都有包括private和public
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        // 指定序列化输入的类型，类必须是非final修饰的，final修饰的类，比如String，Integer等会报异常
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // redis key 序列化方式使用StringSerial
        template.setKeySerializer(stringRedisSerializer);
        // redis hash key 序列化方式使用StringSerial
        template.setHashKeySerializer(stringRedisSerializer);
        // redis value 序列化方式使用jackson
        template.setValueSerializer(jackson2JsonRedisSerializer);
        // redis hash value 序列化方式使用jackson
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }


}
